home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
F1 Licenseware
/
F1 Licenseware - Volume 1.iso
/
disks
/
050a.dms
/
050a.adf
/
TEXTS
/
chapter04.txt
< prev
next >
Wrap
Text File
|
1992-02-26
|
6KB
|
131 lines
The Absolute Beginners Guide To Amos
-------------------------------------
Chapter Four
-------------
Now I think it is time to tackle our first real program, a times table
calculator. Our program will ask the user to type in any number from
1 to 99 and will then print the times table for that number.
Sounds complicated doesn`t it? How many lines do you think all those
possibilities of every table from 1 to 99 will take up? Not many is the
answer, onward >
This time, before I go explaining every command I will show you the main
part of our Times Table Calculator:
LINE INPUT "Please type in a number from 1-99 ";tt
CLS 0: CURS OFF
FOR A=1 TO 12
PRINT A*TT
NEXT A
This, believe it or not, is most of the program, I will now break down each
command:
LINE INPUT "Please type a number from 1-99 ";tt
This strange beast allows the user to pass a value typed in from the
keyboard to our program, the format of LINE INPUT is usually,
LINE INPUT "SOME TEXT IN QUOTES ";variable
------------------------------------------
In our case we want the text to ask the user to type in a number from 1-99,
of course you can re-word the text in any way you wish. Note the space
after the text? This is to keep it neat, you will see what I mean if you
delete the space from this line in EXAMPLE4.Amos, but don`t forget the quotes
around the text just as we do with the PRINT statement.
The quote is then followed by a semi-colon which tells Amos to expect a
variable (remember them?) Our variable which I have called TT will hold the
users input. I could of called TT anything, Amos legal, of course.
Now Amos knows which times table it has to calculate and PRINT to the screen.
CLS 0: CURS OFF
---------------
You should know these commands, check your notes.
FOR A=1 TO 12
-------------
Wow, another weird looking command! This is called a FOR NEXT loop and you
won`t find many programs in Basic that don`t make use of FOR NEXT loops.
They are very powerful and very useful.
A FOR NEXT loop does all the hard work for you. What we are doing is telling
Amos to loop twelve times between the FOR command and the NEXT command
and execute everything in between:
FOR A=1 TO 12 REM Start of loop
do something here REM Execute some commands
NEXT A REM If A is less than 12 INC A and jump backward and
REM execute the command(s) in between. If A=12 then
REM go to the line after NEXT A and continue
REM executing the rest of the program.
Here is an example use of a FOR NEXT loop that I could of used in
Example1.Amos to display CLS 0 to CLS 15:
FOR B=0 TO 15 REM The loop starts at 0 and ends at 15
CLS B REM Each pass of the loop will CLear the Screen in the
REM colour of the loop counter (b), so if B equalled 0
like it would on the very first loop the program
would clear the screen to black. The same as CLS 0
WAIT KEY REM WAIT for the user to press a KEY, giving the user
a chance to see each colour, otherwise it would be
over in a flash!
NEXT B REM When the program reaches here Amos looks at the
value of the loop counter, in this case B to check
if it is equal to the maximum loopage, 15 in this
case. If the loop counter is less than 15 it will
INC B and loop back to the CLS command. When B
eventually reaches 15 the loop will end and Amos
will skip past the NEXT B instruction and execute
the following line. If there is no line then the
program will end.
Don't forget that the loop counter variable can be any legal letters you
choose just like any other variable:
FOR ZASQ=1 TO 60000 FOR T=-8 TO -2 FOR QQ=7 TO 9 FOR LCOUNT=1 TO 6
Etc. Etc.
This is a complex concept to explain and is better seen in action.
FOR NEXT is explained further in EXAMPLE4.Amos. Make sure you change the
values of A to see it`s effects.
Moving on to the next line in our Times Table program we have:
PRINT A*TT
----------
We know about the PRINT command e.g. PRINT "Testing..." But this PRINT is
slightly different. Amos is doing two jobs here, it`s calculating A*TT in
which A is a number starting from 1 and ending at 12 and TT is the users
input number, so let`s presume the user typed in 10 as his/her selected Times
Table and this was the first loop of the FOR NEXT loop
A would =1 A is the loop counter variable
TT would =10 TT is the number the user typed in and the times table we
we have to display.
so A*TT would =10
which is the same as saying PRINT "10", we do not need quotes when printing
a variable, in fact you shouldn`t put quotes as this is what would
happen,
PRINT "A*TT"
------------
On screen you would get the result of,
A*TT
instead of 10, why? Because Amos thinks that A*TT is a word you want printed
and not a calculation you want printed as it was wrapped in quotes.
That`s it for this chapter, you will need to load EXAMPLE4.Amos for more
advice and explanations. Believe me it`s a lot easier to understand that way.
End of chapter four
^^^^^^^^^^^^^^^^^^^